package com.example.exampleproject.presenter;

import android.app.Activity;

import com.example.exampleproject.base.BaseExceptionObserver;
import com.example.exampleproject.base.BaseObserver;
import com.example.exampleproject.base.BasePresenter;
import com.example.exampleproject.datamanager.MainDataManager;
import com.example.exampleproject.entity.BaseResponse;
import com.example.exampleproject.entity.RequestBody;
import com.example.exampleproject.entity.Weather;
import com.example.exampleproject.view.IMainView;
import com.google.gson.Gson;

import javax.inject.Inject;

public class MainPresenter extends BasePresenter<IMainView> implements IMainPresenter {

    private MainDataManager mMainDataManager;
    private Gson mGson;

    @Inject
    public MainPresenter(MainDataManager mainDataManager, Gson gson) {
        mMainDataManager = mainDataManager;
        mGson = gson;
    }

    @Override
    public void attachView(IMainView view) {
        super.attachView(view);
        addSubscription(mMainDataManager.subscribeToGetUser(handleMessage()));
    }

    private BaseObserver<BaseResponse<Weather>> handleMessage() {
        System.out.println(mGson);
        return new BaseExceptionObserver<BaseResponse<Weather>>((Activity)getView()) {
            @Override
            protected void onSuccess(BaseResponse<Weather> baseResponse) {
                getView().showWeather(baseResponse.getData().toString());
            }
        };
    }

    @Override
    public void requestWeather() {
        RequestBody weatherRequest = new RequestBody("39.0933793900", "117.1350458000");
        mMainDataManager.requestWeather(weatherRequest);
    }
}
